//
//  HLTzhuanzhangTableViewCell.m
//  huiliantong
//
//  Created by hlt on 15/9/2.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTzhuanzhangTableViewCell.h"
#import "HLTFriendsTableViewController.h"
@implementation HLTzhuanzhangTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    
    self.biaozhi=0;
    
    
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 初始化时加载collectionCell.xib文件 这个是把这个xib文件全部加载了，然后取第一个
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
        
        // 如果路径不存在，return nil
        if (arrayOfViews.count < 1)
        {
            return nil;
        }
        // 如果xib中view不属于UICollectionViewCell类，return nil
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])
        {
            return nil;
        }
        // 加载nib
        self = [arrayOfViews objectAtIndex:0];
    }
    return self;
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}





- (IBAction)tijiao:(id)sender {
    
    
    
    
    [EYInputPopupView popViewWithTitlenumber:@"请输入登陆密码" contentText:@"登陆密码"
                                        type:EYInputPopupView_Type_single_line_text
                                 cancelBlock:^{
                                     
                                 } confirmBlock:^(UIView *view, NSString *text) {
                                     
                                     
                                     Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                                     
                                     
                                     if ([acc.pwd isEqual:text]) {
                                        
                                         //    &user_name=18285117540&payToUser=18285117086&amount=0.01&beizhu=
                                         
                                         //取消键盘
                                         [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
                                         
                                         if ([self.jine.text floatValue] >=1.0) {
                                             
                                             
                                             
                                             UIView *view1=[[UIView alloc] initWithFrame:self.parent.view.bounds];
                                             view1.backgroundColor=[UIColor grayColor];
                                             view1.alpha=0.4;
                                             [self.parent.view addSubview:view1];
                                             MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
                                             indicatorView.delegate = self;
                                             indicatorView.numberOfCircles = 3;
                                             indicatorView.radius = 20;
                                             indicatorView.internalSpacing = 3;
                                             indicatorView.center = self.parent.view.center;
                                             [indicatorView startAnimating];
                                             [self.parent.view addSubview:indicatorView];
                                             
                                             
                                             
                                             Account *acc=[[AccountTool sharedAccountTool ]currentAccount];
                                             //base64 编码
                                             NSString *base64pwd= [SetBase64andMd5 base64:acc.pwd ncryptOrDecrypt:kCCEncrypt];
                                             
                                             NSString *url=[zhuanzhang stringByAppendingFormat:@"&user_name=%@&payToUser=%@&amount=%@&beizhu=%@&password=%@",acc.custPhone,self.tousername.text,self.jine.text,self.beizhu.text,base64pwd];
                                             
                                             
                                             
                                             [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
                                                 [indicatorView stopAnimating];
                                                 [view1 removeFromSuperview];
                                                 
                                                 
                                                 
                                                 //将nsnumber 装换成nsstring
                                                 NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
                                                 NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
                                                 
                                                 
                                                 if ([str isEqual:@"2"]) {
                                                     
                                                     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"转账成功"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                                                     [alert show];
                                                     
                                                     [self.parent.navigationController popViewControllerAnimated:YES]; //返回上一页面
                                                     
                                                 }
                                                 else{
                                                     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"转账失败"   message:ary[@"msg"] delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                                                     [alert show];
                                                     
                                                 }
                                                 
                                                 
                                                 
                                             } faliure:^(NSString *error) {
                                                 
                                                 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"转账失败"   message:@"请求超时，请查看交易明细是否交易成功"  delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                                                 [alert show];
                                                 
                                                 
                                                 
                                                 
                                                 [indicatorView stopAnimating];
                                                 [view1 removeFromSuperview];
                                             }];
                                             
                                             
                                         }
                                         else{
                                             
                                             UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请重新输入金额"   message:@"限额为1元" delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                                             [alert show];
                                         }
                                         
                                         
                                     }else
                                     {
                                         
                                         
                                         self.biaozhi=self.biaozhi+1;
                                         if (self.biaozhi>=5) {
                                             
                                             UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"提示" message:@"您输入的密码错误次数超过5次，请重新登陆" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                                             
                                             [alert show];
                                             Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                                             //退出登录
                                             [[AccountTool sharedAccountTool] tuichu:acc];
                                             [self.parent.navigationController popViewControllerAnimated:YES];
                                             
                                         }
                                         else
                                         {
                                         
                                             NSString *str=[NSString stringWithFormat:@"您输入的密码不对,还有%ld次输入机会",5-self.biaozhi];
                                             
                                             UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                                             
                                             [alert show];
                                         }
                                     }
                                     
                                     
                                 } dismissBlock:^{
                                     
                                 }];

    
    
    
    
    
    
    
    
    

    
}






- (IBAction)friends:(id)sender {
    UIView *view1=[[UIView alloc] initWithFrame:self.parent.view.bounds];
    view1.backgroundColor=[UIColor grayColor];
    view1.alpha=0.4;
    [self.parent.view addSubview:view1];
    MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.parent.view.center;
    [indicatorView startAnimating];
    [self.parent.view addSubview:indicatorView];
    
    
    
    
    
    Account *acc=[[AccountTool sharedAccountTool ]currentAccount];
     NSString *url=[myfriend stringByAppendingFormat:@"&username=%@",acc.custPhone];
    //将url转换一下格式，否则请求的格式就不兼容
    //    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
        
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        HLTFriendsTableViewController *vc=[[HLTFriendsTableViewController alloc]init];
        vc.friendlist=ary;
        vc.parent=self.parent;
        [self.parent.navigationController pushViewController:vc animated:YES]; //跳转到下一页面
        
    } faliure:^(NSString *error) {
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
    }];
    
    
}



//这个是回调方法
#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}





@end
